package com.example.tourapp.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.tourapp.Adapter.list_Apadter;
import com.example.tourapp.Pojo.Info;
import com.example.tourapp.R;

import org.litepal.LitePal;

import java.util.List;

public class Note extends AppCompatActivity {
    private ListView listview;
    private ImageView img;
    private List<Info> list;
    private list_Apadter apadter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note);
        initView();
        inifunction();
    }
    private void initView() {
        listview = (ListView) findViewById(R.id.listview);
        img = (ImageView) findViewById(R.id.img);
        LitePal.getDatabase();
    }
    private void inifunction() {
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(Note.this,Content_add.class);
                startActivity(intent);
            }
        });
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Info info = list.get(i);
                Intent intent=new Intent(Note.this,Content_updata.class);
                //intent 传入数据
                intent.putExtra("content",info.getContent());
                intent.putExtra("date",info.getDate());
                intent.putExtra("id",info.getId());
                startActivity(intent);
            }
        });
        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
                //显示AlertDialog框
                AlertDialog.Builder dialog=new AlertDialog.Builder(Note.this);
                dialog.setTitle ("删除信息？").setMessage ("您确定要删除这条信息吗？");
                //点击确定就退出程序
                dialog.setPositiveButton ("确定", new DialogInterface.OnClickListener () {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //删除listview内容
                        Info info = list.get(i);
                        int delete = info.delete();
                        if(delete==0){
                            Toast.makeText(Note.this, "删除不成功", Toast.LENGTH_SHORT).show();
                        }
                        list.remove(i);
                        //更新listview
                        apadter.notifyDataSetChanged();
                        Toast.makeText(getBaseContext(), "删除成功", Toast.LENGTH_SHORT).show();
                    }
                });
                //如果取消，就什么都不做，关闭对话框
                dialog.setNegativeButton ("取消",null);
                dialog.show ();
                return false;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        //返回界面刷新数据
        list = LitePal.findAll(Info.class);
        apadter=new list_Apadter(list,this);
        listview.setAdapter(apadter);
    }
}